package org.jeecg.modules.activiti.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.activiti.entity.*;
import org.jeecg.modules.activiti.mapper.FormDesignMapper;
import org.jeecg.modules.activiti.service.IFormDesignService;
import org.jeecg.modules.activiti.util.OrderNoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 流程业务扩展表
 * @author: 黄劲博
 * @date: 2021/6/21 9:50
*/
@Service
public class FormDesignServiceImpl extends ServiceImpl<FormDesignMapper, FormDesign> implements IFormDesignService {


    @Autowired
    private FormDesignMapper formDesignMapper;

    private LoginUser getLoginUser () {
        return (LoginUser) SecurityUtils.getSubject().getPrincipal();
    }

    @Override
    public boolean update(FormDesign formDesign) {
        Integer row = 0;
        FormDesign data = formDesignMapper.selectById(formDesign.getId());
        if (StringUtils.isEmpty(data.getData()) || formDesign.getData().equals(data.getData())) {
            formDesign.setUpdateId(getLoginUser().getId());
            row = formDesignMapper.updateById(formDesign);
        } else {
            formDesign.setId("");
            formDesign.setCreateId(getLoginUser().getId());
            formDesign.setUpdateId(null);
            formDesign.setUpdateTime(null);
            formDesign.setCreateTime(null);
            row = formDesignMapper.insert(formDesign);
            data.setUpdateId(getLoginUser().getId());
            data.setStatus("2");
            row = formDesignMapper.updateById(data);
        }
        if (row == 0)
            return false;
        else
            return true;
    }

    @Override
    public boolean insert(FormDesign formDesign) {
        formDesign.setFromNumber(OrderNoUtils.createRequestNo("",7));
        formDesign.setCreateId(getLoginUser().getId());
        Integer row = formDesignMapper.insert(formDesign);
        if (row == 0)
            return false;
        else
            return true;
    }

    @Override
    public Map<String, Object> getDynamicData() {
        Map<String, Object> res = new HashMap<>();
        LoginUser user = getLoginUser();
        res.put("currUserName", user.getRealname());
        res.put("currUserID", user.getId());
        res.put("currUserDeptName", "技术部");
        res.put("currUserDeptID", user.getOrgCode());

        return res;
    }
}
